package com.atguigu.baseservice.handler;

import com.atguigu.commonutils.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice  //统一异常处理器
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class) //抓异常
    @ResponseBody  //用数据方式返回
    public R error(Exception e){
        e.printStackTrace();
        return R.error();
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody  //用数据方式返回
    public R error(ArithmeticException e){
        e.printStackTrace();
        return R.error().message("执行了by zero自定义异常");
    }


    @ExceptionHandler(GuliException.class)
    @ResponseBody  //用数据方式返回
    public R error(GuliException e){
        e.printStackTrace();
        return R.error().message(e.getMsg()).code(e.getCode());
    }
}
